package com.yjt.sousou.utils;

import android.content.Context;
import android.view.View;

import com.yjt.sousou.R;
import com.yjt.sousou.widget.wheelview.Constant;
import com.yjt.sousou.widget.wheelview.OnWheelChangedListener;
import com.yjt.sousou.widget.wheelview.OnWheelScrollListener;
import com.yjt.sousou.widget.wheelview.WheelView;
import com.yjt.sousou.widget.wheelview.adapter.NumericWheelAdapter;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 作者：chs on 2016/12/26 17:38
 * 邮箱：657083984@qq.com
 */

public class WheelViewUtil {
    private WheelView wl_year;
    private WheelView wl_month;
    private WheelView wl_day;
    private WheelView wl_hour;
    private WheelView wl_min;
    private OnWheelViewScrollListener mOnWheelViewScrollListener;
    private Context mContext;
    private int curYear;
    private int curMonth;
    private int hours;
    private int minute;
    private String hourValue = "00";
    private String minValue = "00";
    private String dateType = "";
    private int textSize = 16;
    public void setOnWheelViewScrollListener(OnWheelViewScrollListener onWheelViewScrollListener) {
        mOnWheelViewScrollListener = onWheelViewScrollListener;
    }

    public interface OnWheelViewScrollListener {
         void result(int year, int month, int day);
    }
    public WheelViewUtil(Context context,View view) {
        this.mContext = context;
        initWheelView(view,context);
    }

    public WheelViewUtil(Context context, View view, String dateType, int width) {
        this.mContext = context;
        this.dateType = dateType;
        setTextSize(width);
        initWheelView(view,context);
    }

    private void setTextSize(int width) {
        if (width != 0){
            if (width <= 500&&dateType.equals("1")) {
                textSize = 14;
            }else if(width<750&&dateType.equals("1")){
                textSize = 16;
            }
        }
    }

    private void initWheelView(View view, Context context) {
        Calendar c = Calendar.getInstance();
        curYear = c.get(Calendar.YEAR);
        curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        int curDate = c.get(Calendar.DATE);
        hours = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);
        wl_year = (WheelView) view.findViewById(R.id.wl_year);
        wl_month = (WheelView) view.findViewById(R.id.wl_month);
        wl_day = (WheelView) view.findViewById(R.id.wl_day);
        wl_hour = (WheelView) view.findViewById(R.id.wl_hour);
        wl_min = (WheelView) view.findViewById(R.id.wl_min);

        NumericWheelAdapter numericWheelAdapterStart1 = new NumericWheelAdapter(context, 2000, 2100);
        numericWheelAdapterStart1.setLabel("年");
        wl_year.setViewAdapter(numericWheelAdapterStart1);
        wl_year.setVisibleItems(5);
//        numericWheelAdapterStart1.setTextColor(R.color.black);
        numericWheelAdapterStart1.setTextSize(textSize);
        wl_year.setCyclic(true);//是否可循环滑动
        wl_year.addScrollingListener(startScrollListener);
        wl_year.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                curYear = newValue+2000;
                initDayAdapter();
            }
        });
        NumericWheelAdapter numericWheelAdapterStart2 = new NumericWheelAdapter(context, 1, 12, "%02d");
        numericWheelAdapterStart2.setLabel("月");
        wl_month.setViewAdapter(numericWheelAdapterStart2);
        wl_month.setVisibleItems(5);
//        numericWheelAdapterStart2.setTextColor(R.color.black);
        numericWheelAdapterStart2.setTextSize(textSize);
        wl_month.setCyclic(true);
        wl_month.addScrollingListener(startScrollListener);
        wl_month.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                curMonth = newValue+1;
                initDayAdapter();
            }
        });
        if(wl_day!=null){
            NumericWheelAdapter numericWheelAdapterStart3 = new NumericWheelAdapter(context, 1, GetTimeUtil.getDay(curYear, curMonth), "%02d");
            numericWheelAdapterStart3.setLabel("日");
            wl_day.setViewAdapter(numericWheelAdapterStart3);
            wl_day.setVisibleItems(5);
//            numericWheelAdapterStart3.setTextColor(R.color.black);
            numericWheelAdapterStart3.setTextSize(textSize);
            wl_day.setCyclic(true);
            wl_day.addScrollingListener(startScrollListener);
        }

        if(wl_hour!=null){
            NumericWheelAdapter numericAdapter1 = new NumericWheelAdapter(context, 0, 23,"%02d");
            numericAdapter1.setLabel(" :");
            numericAdapter1.setTextSize(textSize);
            wl_hour.setViewAdapter(numericAdapter1);
            wl_hour.setVisibleItems(5);
            wl_hour.setCyclic(true);// 可循环滚动
            wl_hour.addChangingListener(new OnWheelChangedListener() {
                @Override
                public void onChanged(WheelView wheel, int oldValue, int newValue) {
//                int hour = wl_hour.getCurrentItem();
                    hourValue = Constant.XIAOSHI[newValue];

                }
            });
            wl_hour.setCurrentItem(hours);
        }
        if(wl_min!=null){
            NumericWheelAdapter minAdapter1 = new NumericWheelAdapter(context, 0, 59,"%02d");
            minAdapter1.setLabel("");
            minAdapter1.setTextSize(textSize);
            wl_min.setViewAdapter(minAdapter1);
            wl_min.setVisibleItems(5);
            wl_min.setCyclic(true);// 可循环滚动
            wl_min.addChangingListener(new OnWheelChangedListener() {
                @Override
                public void onChanged(WheelView wheel, int oldValue, int newValue) {
                    minValue = Constant.FENZHONG_START[newValue];
                }
            });
            wl_min.setCurrentItem(minute);
        }

        wl_year.setCurrentItem(curYear - 2000);
        wl_month.setCurrentItem(curMonth - 1);
        if(wl_day!=null)
        wl_day.setCurrentItem(curDate - 1);

        if(dateType.equals("1")){
            wl_hour.setVisibility(View.VISIBLE);
            wl_min.setVisibility(View.VISIBLE);

            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
            String currentTime = sDateFormat.format(new Date());
            String currenthh = currentTime.substring(11, 13);
            List<String> asList = Arrays.asList(Constant.XIAOSHI);
            int hour_index = asList.indexOf(currenthh);
            wl_hour.setCurrentItem(hour_index);

            String currentmm = currentTime.substring(14);
            List<String> asList2 = Arrays.asList(Constant.FENZHONG_START);
            int min_index = asList2.indexOf(currentmm);
            wl_min.setCurrentItem(min_index);

        }
    }
    private OnWheelScrollListener startScrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {
        }
        @Override
        public void onScrollingFinished(WheelView wheel) {
            int n_year = wl_year.getCurrentItem() + 2000;//年
            int n_month = wl_month.getCurrentItem() + 1;//月
            int n_day = wl_day!=null?wl_day.getCurrentItem() + 1:0;//日
            if(mOnWheelViewScrollListener!=null){
                mOnWheelViewScrollListener.result(n_year,n_month,n_day);
            }
        }
    };
    private void initDayAdapter(){
        if(wl_day!=null){
        NumericWheelAdapter numericWheelAdapterStart3=new NumericWheelAdapter(mContext,1,GetTimeUtil.getDay(curYear,curMonth), "%02d");
        numericWheelAdapterStart3.setLabel("日");
        wl_day.setViewAdapter(numericWheelAdapterStart3);
//        numericWheelAdapterStart3.setTextColor(R.color.black);
        numericWheelAdapterStart3.setTextSize(textSize);
        wl_day.setCyclic(true);
        wl_day.addScrollingListener(startScrollListener);
        }
    }
    public String getCurrentDate(){
        int n_year = wl_year.getCurrentItem() + 2000;//年
        int n_month = wl_month.getCurrentItem() + 1;//月
        int n_day = wl_day.getCurrentItem() + 1;//日
        return n_year + "-" + n_month + "-" + n_day;
    }
    public String getCurrentDateAndTime(){
        int n_year = wl_year.getCurrentItem() + 2000;//年
        int n_month = wl_month.getCurrentItem() + 1;//月
        int n_day = wl_day.getCurrentItem() + 1;//日
        return n_year + "-" + n_month + "-" + n_day +" " +hourValue+":" +minValue;
    }

    public String getCurrentDateAndTime2(){
        int n_year = wl_year.getCurrentItem() + 2000;//年
        int n_month = wl_month.getCurrentItem() + 1;//月
        int n_day = wl_day.getCurrentItem() + 1;//日
        return n_year + "." + n_month + "." + n_day +" " +hourValue+":" +minValue;
    }

    public String getCurrentDateD(){
        int n_year = wl_year.getCurrentItem() + 2000;//年
        int n_month = wl_month.getCurrentItem() + 1;//月
        int n_day = wl_day.getCurrentItem() + 1;//日
        return n_year + "." + n_month + "." + n_day;
    }
    public String getCurrentDateZ(){
        int n_year = wl_year.getCurrentItem() + 2000;//年
        int n_month = wl_month.getCurrentItem() + 1;//月
        String month = String.format("%02d",n_month);
//        int n_day = wl_day.getCurrentItem() + 1;//日
        return n_year + "年" + month + "月";
    }
    public String getCurrentDateYM(){
        int n_year = wl_year.getCurrentItem() + 2000;//年
        int n_month = wl_month.getCurrentItem() + 1;//月
        String month = String.format("%02d",n_month);
//        int n_day = wl_day.getCurrentItem() + 1;//日
        return n_year + "-" + month;
    }
}
